﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using DevExpress.XtraBars.ToolbarForm;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraPivotGrid;
using XCI.Helper;
using XCI.Setting.Controls;
using XCI.Windows;

namespace XCI.Setting
{
    public partial class FrmSettingPivotGridForm : ToolbarForm
    {
        private PivotGridControlPlus gridControl;

        public FrmSettingPivotGridForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (WinFormHelper.IsDesignMode()) WinFormHelper.InitDesignFont();
        }

        public void Init(PivotGridControlPlus gridControl)
        {
            this.gridControl = gridControl;
            this.barId.EditValue = gridControl.Id;
            InitControls();
            TabDelayLoad.Init(tabPane1, name =>
            {
                switch (name)
                {
                    case nameof(tabFields):
                        settingPivotGridFields1.Init(this.gridControl);
                        break;
                    case nameof(tabAppearance):
                        gridAppearance1.Init(this.gridControl.Appearance);
                        break;
                    case nameof(tabExpression):
                        settingFormatCondition1.Init();
                        break;
                    case nameof(tabTemplate):
                        gridTemplate1.Init(this.gridControl.SettingManager, "pivot_grid_:_");
                        break;
                    case nameof(tabAdvanced):
                        gridAdvanced1.SelectedObject = this.gridControl;
                        break;
                }
            });
        }

        private void InitControls()
        {
            #region gridFormatCondition1
            settingFormatCondition1.GetConditions = () => gridControl.FormatConditions;
            settingFormatCondition1.AddCondition = () =>
            {
                var condition = new PivotGridStyleFormatCondition { Condition = FormatConditionEnum.Expression };
                gridControl.FormatConditions.Add(condition);
                return condition;
            };
            settingFormatCondition1.GetColumnInfos = () =>
            {
                var colInfos = new List<ColumnInfo>();
                foreach (PivotGridField col in gridControl.Fields)
                {
                    string name = string.IsNullOrEmpty(col.Caption) ? col.FieldName : col.Caption;
                    colInfos.Add(new ColumnInfo { Name = name, Column = col });
                }
                return colInfos;
            };
            #endregion
        }

        private void Form_Load(object sender, EventArgs e)
        {

        }

        private async void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置表格配置吗？重置后此表格的配置会全部删除") != DialogResult.Yes) return;

            await gridControl.SettingManager.DeleteAllAsync();
            this.Close();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await gridControl.SettingManager.SaveCurrentAsync();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复表格默认配置吗？恢复会丢失当前配置") != DialogResult.Yes) return;

            await gridControl.SettingManager.LoadDefaultAsync();
            MessageBoxHelper.ShowMessage("恢复表格默认配置成功,请点保存确认");
            this.Close();
        }

        private async void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前配置设为默认配置吗？") != DialogResult.Yes) return;
            await gridControl.SettingManager.SaveDefaultAsync();
            MessageBoxHelper.ShowMessage("默认配置设置成功");
        }
    }
}